﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using h = functionService.shorts;
using da = functionService.DATECOD;
using vv = functionService.serfun_city7;
namespace ETALON
{
    namespace DLGT
    {
        enum modeleg { tovkol,kol, noDelegate}
        enum pe_param { what_select_nameCod_1=1, kol_2=2}
        public delegate void selectTovKol();
    }
    public partial class FormTreeSelect : Form
    {
        DataTable df; bool IsSelectNode; DLGT.selectTovKol ds; DLGT.modeleg curMOD;
        //fs.prm_city q
        functionService.prm_city q; functionService.prm pe;
        DB_SQL.ConnectionDB_SQL c;
        const string LEV_1 = "lev", COD_2 = "cod", FO_3 = "fo";
        const int iLEV_1 = 0, iCOD_2 = 1, iFO_3 = 2;
        const int df_fields_lev_1 = 1, df_fields_cod_2 = 2, df_fields_name_3 = 3, df_fields_cop_4 = 4, df_fields_fo_5 = 5;
 
        string currentNode_tag;
        string TABLENAME, SCHEMA;

        public FormTreeSelect()
        {
            InitializeComponent(); setWindowPosition_const();
        }
        public FormTreeSelect(string schemaName, string tableName)
            : this()
        {
            curMOD = ETALON.DLGT.modeleg.noDelegate ;
            InitializeComponentMy(schemaName, tableName); 
        }
        public FormTreeSelect(string schemaName, string tableName, DLGT.selectTovKol dso)
            : this()
        {
            curMOD = ETALON.DLGT.modeleg.tovkol;
            ds = dso; InitializeComponentMy(schemaName, tableName); 
        }
        
        public FormTreeSelect(string schemaName, string tableName, DLGT.selectTovKol dso, bool IsCalculateVisible)
            : this()
        {
            curMOD = ETALON.DLGT.modeleg.tovkol ;
            ds = dso; InitializeComponentMy(schemaName, tableName); 
            
            doCalculateVisible( IsCalculateVisible);
            
        }
        
        public void doCalculateVisible(bool isv)
        {
            toolStripStatusLabel2.Visible = isv;
            toolStripStatusLabel3.Visible = isv;
            toolStripStatusLabel4.Visible = isv;
            toolStripStatusLabel5.Visible = isv;
            toolStripStatusLabel6.Visible = isv;
            toolStripStatusLabel7.Visible = isv;
            toolStripStatusLabel8.Visible = isv;
            toolStripStatusLabel9.Visible = isv;
            toolStripStatusLabel10.Visible = isv;
            toolStripStatusLabel1.Visible = isv;
            toolStripStatusLabel13.Visible = isv;
        }
        public void setWindowPosition_const()
        {
              this.Left = 0; this.Top = 95; this.Width = 450; this.Height = 500;
            //this.Left = 0; this.Top = 95; this.Width = q.tk_int(param.tree_position_Width_35); this.Height = q.tk_int(param.tree_position_Height_36);
        }
        public void setWindowPosition()
        {
         //   this.Left = 0; this.Top = 95; this.Width = 450; this.Height = 500;
            this.Left = 0; this.Top = 95;
            if (TABLENAME == "listtable")
            {
                this.Width = q.tk_int(param.tree_position_Width_35);
                this.Height = q.tk_int(param.tree_position_Height_36);
            }
            else if (TABLENAME == "listtov")
            {
                this.Width = q.tk_int(param.tree_position_Width_TOV_37);
                this.Height = q.tk_int(param.tree_position_Height_36);
            }
        }
        #region ini
        void InitializeComponentMy(string schemaName, string tableName)
        {
            SCHEMA = schemaName;
            TABLENAME = tableName;
            q = new functionService.prm_city(); q.con.co(q.tk(param.server_1));
            pe = new functionService.prm("PLP_treeSelect");
           // q= new SERVICE(); q.co();
            
            df = q.selectAny(functionService.shorts.a("call `city`.`select_treea`('",SCHEMA,"', '", TABLENAME ,"');")); //q.sh(df);
            df_load();
            IsSelectNode = false;
            currentNode_tag = "0;0;0"; 
            this.Text = "Список";//q.tc(param.default_3);
            //pr.put((int)param.default_3, "Стол"); 
            //curKol = "1";
            //q.put(param.zakaz_kol_17, curKol);
            vv.kol = 1;
           // toolStripStatusLabel2.Text = functionService.shorts.aa("Кол:", f.valueEnter);
            setWindowPosition();
        }
        void df_load()
        {
            if (df.Rows.Count > 0)
            {
                readTableParent_readTableChild_0_lev(treeView1, df);
            }
        }
        private void readTableParent_readTableChild_0_lev(TreeView tv, DataTable dfo)
        {
            //q.sh(tp); q.sh(tc);

            int levo = 0;
            DataView p = new DataView(df); p.RowFilter = functionService.shorts.a("lev=", levo);
            DataView c = new DataView(df); c.RowFilter = functionService.shorts.a("lev=", levo + 1);
            string at = "";
            int ip = p.Count, ic = c.Count;

            //string 
            // setHTA(hta,0,0,0);

            TreeNode nn, nnn;

            for (int i = 0; i < ip; i++)
            {
                nn = new TreeNode(functionService.nameCod.name_cod(p[i][3], p[i][2]));
                c.RowFilter = string.Concat("cop = ", p[i][2], " and lev =", levo + 1);
                for (int j = 0; j < c.Count; j++)
                {
                    at = functionService.shorts.a(c[j][df_fields_lev_1], ";", c[j][df_fields_cod_2], ";", c[j][df_fields_fo_5]);
                    nnn = new TreeNode(functionService.nameCod.name_cod(c[j][df_fields_name_3], c[j][df_fields_cod_2])); nnn.Tag = at;
                    nn.Nodes.Add(nnn);
                }
                at = functionService.shorts.a(p[i][df_fields_lev_1], ";", p[i][df_fields_cod_2], ";", p[i][df_fields_fo_5]); nn.Tag = at;
                tv.Nodes.Add(nn);
            }
        }


        void afterExp()
        {
            if (IsSelectNode)
            {

                
                string ag;
                int indexsel = treeView1.SelectedNode.Index;
                string st = treeView1.SelectedNode.Text;

                ag = treeView1.SelectedNode.Tag.ToString();
                int lev = Convert.ToInt32(readTAG(ag, iLEV_1));
                object oc = readTAG(ag, iCOD_2);

                #region LOAD---

                Level_any_readTableParent_readTableChild
                    (
                    treeView1.SelectedNode, indexsel, oc,
                    lev
                    );



                #endregion

                IsSelectNode = false;
            }
        }
        private void Level_any_readTableParent_readTableChild(TreeNode tno,
         int indexSelectNode, object objectCodParent, int levo)
        {
            DataView p = new DataView(df);
            p.RowFilter = string.Concat("cop = ", objectCodParent, " and lev =", levo + 1);

            int ip = p.Count; //fs.shorts.m(fs.shorts.aa(ip, "=ip"));

            

            DataView c = new DataView(df); //c.RowFilter = string.Concat("cop = ", objectCodParent, " and lev =", levo);
            TreeNode nn, nnn;
            string ag = "";
            //Hashtable hta = new Hashtable(); setHTA(hta, 0,0,0);

            tno.Nodes.Clear();
            for (int i = 0; i < ip; i++)
            {
                //  setHTA(hta, p[i][iLEV_1], p[i][iCOD_2], p[i][iFO_3]);


                nn = new TreeNode(functionService.nameCod.name_cod(p[i][3], p[i][2]));
                c.RowFilter = string.Concat("cop = ", p[i][2], " and lev = ", levo + 2);

                for (int j = 0; j < c.Count; j++)
                {
                    //setHTA(hta, c[j][iLEV_1], c[j][iCOD_2], c[j][iFO_3]);
                    ag = functionService.shorts.a(c[j][1], ";", c[j][2], ";", c[j][5]);
                    nnn = new TreeNode(functionService.nameCod.name_cod(c[j][3], c[j][2])); nnn.Tag = ag; nnn.ImageIndex = imageIndex(c[j][5], true); nnn.SelectedImageIndex = imageIndex(c[j][5], false);
                    nn.Nodes.Add(nnn);
                }
                ag = functionService.shorts.a(p[i][1], ";", p[i][2], ";", p[i][5]); nn.Tag = ag; nn.ImageIndex = imageIndex(p[i][5], true); nn.SelectedImageIndex = imageIndex(p[i][5], false);
                tno.Nodes.Add(nn);
            }



        }

        private int imageIndex(object pi5, bool true_no_select)
        {
            int ir = 0;
            int[] aObj = new int[] { 10, 11 }; int[] aFld = new int[] { 8, 9 };
            if (pi5.ToString() == "1")
            {
                if (true_no_select) ir = aObj[0]; else ir = aObj[1];
            }
            else
            {
                if (true_no_select) ir = aFld[0]; else ir = aFld[1];
            }
            return ir;
        }
        string readTAG(string tago, int indexo)
        {
            string[] sa = tago.Split(';'); return sa[indexo];
        }
        string readTAG(object tago, int indexo)
        {
            string[] sa = tago.ToString().Split(';'); return sa[indexo];
        }
        int readTAGi(object tago, int indexo)
        {
            string[] sa = tago.ToString().Split(';'); return Convert.ToInt32(sa[indexo]);
        }
        #endregion

        private void treeView1_AfterExpand(object sender, TreeViewEventArgs e)
        {
            afterExp();
        }

        private void treeView1_DoubleClick(object sender, EventArgs e)
        {

        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            
            IsSelectNode = true; //fs.shorts.m(treeView1.SelectedNode.Tag);
            if (IsObject)
            {
                string ss = treeView1.SelectedNode.Text, ssc = functionService.nameCod.readCod(ss); 
                q.put(param.treeView_select_14, ss);  pe.put((int)DLGT.pe_param.what_select_nameCod_1, ss);
                if (curMOD == ETALON.DLGT.modeleg.tovkol) { ds(); }
            }
        }

        private bool IsObject
        {
            get 
            {
                int iob = readTAGi(treeView1.SelectedNode.Tag, iFO_3);  if (iob == 1) return true; else return false;
            }
        }

        

        private void toolStripStatusLabel2_MouseDown(object sender, MouseEventArgs e)
        {
            toolStripStatusLabel2.ForeColor = Color.White;
        }

        private void toolStripStatusLabel2_MouseUp(object sender, MouseEventArgs e)
        {
            FormKolZakaz f = new FormKolZakaz(); f.ShowDialog();
            //q.put(param.zakaz_kol_17, f.valueEnter);
            //vv.kol = f.valueEnter;
            putkol(Convert.ToInt32(f.valueEnter));
           // toolStripStatusLabel2.Text = functionService.shorts.aa("Кол:", f.valueEnter);
        }

        private void toolStripStatusLabel3_MouseDown(object sender, MouseEventArgs e)
        {
            toolStripStatusLabel3.ForeColor = Color.White;
        }

        private void toolStripStatusLabel4_MouseDown(object sender, MouseEventArgs e)
        {
            toolStripStatusLabel4.ForeColor = Color.White;
        }

        private void toolStripStatusLabel5_MouseDown(object sender, MouseEventArgs e)
        {
            toolStripStatusLabel5.ForeColor = Color.White;
        }

        private void toolStripStatusLabel6_MouseDown(object sender, MouseEventArgs e)
        {
            toolStripStatusLabel6.ForeColor = Color.White;
        }

        private void toolStripStatusLabel7_MouseDown(object sender, MouseEventArgs e)
        {
            toolStripStatusLabel7.ForeColor = Color.White;
        }

        private void toolStripStatusLabel8_MouseDown(object sender, MouseEventArgs e)
        {
            toolStripStatusLabel8.ForeColor = Color.White;
        }

        private void toolStripStatusLabel9_MouseDown(object sender, MouseEventArgs e)
        {
            toolStripStatusLabel9.ForeColor = Color.White;
        }

        private void toolStripStatusLabel10_MouseDown(object sender, MouseEventArgs e)
        {
            toolStripStatusLabel10.ForeColor = Color.White;
        }
        private void putkol(int ikol)
        {
            //q.put(param.zakaz_kol_17, ikol); 
            vv.kol = ikol;
            //curKol = ikol.ToString();
            //pe.put(2, ikol);
            toolStripStatusLabel2.Text = functionService.shorts.aa("Кол:", ikol);
        }
        private void toolStripStatusLabel3_MouseUp(object sender, MouseEventArgs e)
        {
            toolStripStatusLabel3.ForeColor = Color.Black; putkol(1);
        }

        private void toolStripStatusLabel4_MouseUp(object sender, MouseEventArgs e)
        {
            toolStripStatusLabel4.ForeColor = Color.Black; putkol(2);
        }

        private void toolStripStatusLabel5_MouseUp(object sender, MouseEventArgs e)
        {
            toolStripStatusLabel5.ForeColor = Color.Black; putkol(3);
        }

        private void toolStripStatusLabel6_MouseUp(object sender, MouseEventArgs e)
        {
            toolStripStatusLabel6.ForeColor = Color.Black; putkol(4);
        }

        private void toolStripStatusLabel7_MouseUp(object sender, MouseEventArgs e)
        {
            toolStripStatusLabel7.ForeColor = Color.Black; putkol(5);
        }

        private void toolStripStatusLabel8_MouseUp(object sender, MouseEventArgs e)
        {
            toolStripStatusLabel8.ForeColor = Color.Black; putkol(6);
        }

        private void toolStripStatusLabel9_MouseUp(object sender, MouseEventArgs e)
        {
            toolStripStatusLabel9.ForeColor = Color.Black; putkol(7);
        }

        private void toolStripStatusLabel10_MouseUp(object sender, MouseEventArgs e)
        {
            toolStripStatusLabel10.ForeColor = Color.Black; putkol(8);
        }

        private void toolStripStatusLabel11_MouseDown(object sender, MouseEventArgs e)
        {
           
        }

        private void toolStripStatusLabel11_MouseUp(object sender, MouseEventArgs e)
        {
            
        }

        private void toolStripStatusLabel12_MouseDown(object sender, MouseEventArgs e)
        {

            toolStripStatusLabel12.ForeColor = Color.White;
        }

        private void toolStripStatusLabel12_MouseUp(object sender, MouseEventArgs e)
        {
            toolStripStatusLabel12.ForeColor = Color.Black; this.Close();
        }

        private void toolStripStatusLabel12_Click(object sender, EventArgs e)
        {
            object oh = this.Height, ow = this.Width;
            if (TABLENAME == "listtable")
            {
                q.put(param.tree_position_Height_36, oh);
                q.put(param.tree_position_Width_35, ow);
            }
            else if (TABLENAME == "listtov")
            {
                //this.Width = q.tk_int(param.tree_position_Width_TOV_37);
                //this.Height = q.tk_int(param.tree_position_Height_36);
                q.put(param.tree_position_Height_36, oh);
                q.put(param.tree_position_Width_TOV_37, ow);
                //q.put(param.zakaz_kol_17,1);
                vv.kol = 1;
            }
            

            this.Close();
        }

        private void toolStripStatusLabel2_Click(object sender, EventArgs e)
        {

        }

        private void toolStripStatusLabel3_Click(object sender, EventArgs e)
        {

        }

        private void toolStripStatusLabel13_MouseUp(object sender, MouseEventArgs e)
        {
            toolStripStatusLabel13.ForeColor = Color.Black; putkol(9);
        }

        private void toolStripStatusLabel13_MouseDown(object sender, MouseEventArgs e)
        {
            toolStripStatusLabel13.ForeColor = Color.White;
        }

       
    }
}
